package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.ThreadLocalLoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    AttrValueMapper attrValueMapper;

    @Autowired
    FacilityInfoMapper facilityInfoMapper;

    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    PaymentTypeMapper paymentTypeMapper;

    @Autowired
    FeeValueMapper feeValueMapper;

    @Autowired
    LeaseTermMapper leaseTermMapper;

    @Autowired
    LeaseAgreementMapper leaseAgreementMapper;


    @Autowired
    BrowsingHistoryMapper browsingHistoryMapper;


    @Autowired
    BrowsingHistoryService browsingHistoryService;



    @Override
    public IPage<RoomItemVo> selectPageList(IPage<RoomItemVo> iPage, RoomQueryVo queryVo) {
        return roomInfoMapper.selectPageList(iPage, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        Long userId = ThreadLocalLoginUserContext.getLoginUser().getUserId();

        // 插入/更新浏览记录
        browsingHistoryService.saveHistory(userId, id);

//        BrowsingHistory browsingHistory =new BrowsingHistory();
//        browsingHistory.setRoomId(id);
//        browsingHistory.setUserId(userId);
//        browsingHistory.setBrowseTime(new Date());
//
//
//        browsingHistoryMapper.insert(browsingHistory);











        RoomInfo roomInfo = roomInfoMapper.selectById(id);

        ApartmentItemVo apartmentItemVo = apartmentInfoMapper.selectListByRoomId(roomInfo.getApartmentId());

        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper =new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM)
                .eq(GraphInfo::getItemId, id);
        // 获取图片
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList =new ArrayList<>();
        for (GraphInfo graphInfo : graphInfos) {
            GraphVo graphVo =new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            graphVoList.add(graphVo);
        }

        //获取属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);

        //获取配套信息
        List<FacilityInfo> facilityInfoList =facilityInfoMapper.selectListByRoomId(id);

        //获取标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);

        //获取支付方式
        List<PaymentType> paymentTypeList =paymentTypeMapper.selectListByRoomId(id);

        //获取杂费列表
        List<FeeValueVo> feeValueVoList =feeValueMapper.selectListByRoomId(roomInfo.getApartmentId());

        //获取租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);

        //判断是否下架
        boolean isRemove = roomInfo.getIsRelease().getCode() == 1;

        //判断是否入驻
        LambdaQueryWrapper<LeaseAgreement> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LeaseAgreement::getRoomId, id);
        Long count = leaseAgreementMapper.selectCount(lambdaQueryWrapper);
        boolean isCheckIn = count > 0;

        RoomDetailVo roomDetailVo =new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsDelete(isRemove);
        roomDetailVo.setIsCheckIn(isCheckIn);
        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> selectPageById(IPage<RoomItemVo> iPage, Long id) {
        return roomInfoMapper.selectPageById(iPage,id);
    }
}




